
import unittest
import tests.test_student_parser as test_student_parser
from tests import test_persistence
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.runtime import DeadlineExceededError
import StringIO


class MainPage(webapp.RequestHandler):
    def get(self):

        test_cases = [test_student_parser.TestStudentsParser, test_persistence.TestPersistence]
        #test_cases = [test_persistence.TestPersistence]


        suite = unittest.TestSuite()
        output = StringIO.StringIO()
        for test_class in test_cases:
            tests = unittest.TestLoader().loadTestsFromTestCase(test_class)
            suite.addTests(tests)

            unittest.TextTestRunner(output, verbosity=3).run(suite)

        result = output.getvalue()
        temp = result.replace("ERROR", "<span style='color:red; font-weight:bold;'>ERROR</span>")
        result = temp.replace("ok", "<span style='color:green; font-weight:bold'>OK</span>")


        self.response.out.write("""<pre style="white-space:pre-wrap; line-height:1.5em; font:0.75em/1.7em Verdana, Helvetica, sans-serif;">""")
        self.response.out.write(result)
        self.response.out.write("</pre>")


application = webapp.WSGIApplication(
                                     [('/runtests', MainPage)],
                                     debug=True)

def main():
    run_wsgi_app(application)



if __name__ == "__main__":
    main()
